import { reactive, ref } from 'jsdom';
const {JSDOM} = jsdom;



function runRtfjs(rtf, callback, errorCallback) {
    const virtualConsole = new jsdom.VirtualConsole();
    virtualConsole.sendTo(console);
    new JSDOM(`
    <script>
        RTFJS.loggingEnabled(false);
        WMFJS.loggingEnabled(false);
        EMFJS.loggingEnabled(false);

        try {
            const doc = new RTFJS.Document(rtfFile);

            const meta = doc.metadata();
            doc.render().then(function(htmlElements) {
                const div = document.createElement("div");
                div.append(...htmlElements);

                window.done(meta, div.innerHTML);
            }).catch(error => window.onerror(error))
        } catch (error){
            window.onerror(error)
        }
    </script>
    `, {
        resources: "usable",
        runScripts: "dangerously",
        url: "file://" + __dirname + "/",
        virtualConsole,
        beforeParse(window) {
            window.rtfFile = stringToArrayBuffer(rtf);
            window.done = function (meta, html) {
                callback(meta, html);
            };
            window.onerror = function (error) {
                errorCallback(error)
            };
        }
    });
}

export default{ runRtfjs }